﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using lo = sqCore.sqLogger;
using MongoDB.Bson;
using MongoDB.Driver;

namespace sqCore
{
    public class Test
    {

        static void Main(string[] args)
        {
            /*sqDB mongoDB = new sqDB("mongoS", "127.0.0.1", "SqlA", Port: "27017");
            QueryDocument query = new QueryDocument() { { "CurrentJobs.Query", "SELECT TOP 15 [LogTime],[TaskName],[LogMessage] FROM [SqlADataBase].[dbo].[sqLog]" } };
            sqMongoQueryJob job = new sqMongoQueryJob(mongoDB, query, "TaskList");
            job.executeJob();
            sqQueryTreeResult results = (sqQueryTreeResult)job.getResult();
            FileStream fs;
            fs = new FileStream(@"c:\temp\data3.html", FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(fs);
            writer.Write(results.makeHtmlMessage("lala"));
            writer.Close();
            Console.ReadLine();
        }
    }
}*/
            lo.writeLog(LogMessage: "SqlA Start!");
            string[] r=new string[1];
            r[0] = "sqlasystem@gmail.com";
            sqMail myMail = new sqMail(Recipients: r, From: "sqlasystem@gmail.com", MailSubject: "This is a simple task.");
            sqDB SqlADB = new sqDB("SqlADB", "localhost", "SqlA", "Mongo", "27017", "sqla", "sqlsql");
            var job = new sqMongoQueryJob<sqLog>(DB: SqlADB, CollectionName:"Logs", Limit:100);
            //job.executeJob();
            //job.QueryResult.ToJson();
            Console.ReadLine();
            var t = new sqTask("SimpleTasK","SqlA",new sqJob[1]{job},MailTamplate:myMail,MailDefualtText:"This is a simple task.",SendIfNoDiff:true);
            //t.runTask();                        
                        
            MongoClient client = new MongoClient("mongodb://localhost:27017/SqlA");
            MongoServer server = client.GetServer();       
            MongoDatabase db = server.GetDatabase("SqlA");
            MongoCollection<sqTask> myTasks = db.GetCollection<sqTask>("TaskList");            
            myTasks.Insert(t);
            /*MongoCursor<sqTask> cur = myTasks.FindAllAs<sqTask>();
            foreach (sqTask task in cur)
            {
                Console.WriteLine();
            }
            Console.ReadLine();*/
        }
    }
}
            /*sqDB mydb = new sqDB("SqlAData", ".", "SqlADataBase", "MSSQL", "1521", "SQLA", "sqlsql");
            sqJob job1 = new sqQueryJob(mydb);
            ((sqQueryJob)job1).Query = "SELECT TOP 1000 [Type],[Name],[Host],[ServiceName],[Port],[UserName],[Password],[InsertionTime] FROM [SqlADataBase].[dbo].[sqDBList]";
            job1.executeJob();
            string html = ((sqQueryJob)job1).QueryResult.makeHtmlMessage("bla");
            FileStream fs;
            fs = new FileStream(@"c:\temp\data3.html", FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(fs);
            writer.Write(html);
            writer.Close();
            string[] r=new string[1];
            r[0]="yaronana@gmail.com";
            sqMail myMail = new sqMail(HTMLParams: html, Recipients: r, From: "yaron.facebook@gmail.com", MailSubject: "try");
            myMail.sendThroughSMTP();
            

            /*sqDB mydbbb = new sqDB("dbr", "localhost", "dbr");
            string lol = JsonConvert.SerializeObject(mydbbb);
            sqDB newdb  = JsonConvert.DeserializeObject<sqDB>(lol);
            Console.WriteLine(lol);
            Console.ReadLine();
            //string[] r=new string[1];
            //r[0]="yaronana@gmail.com";
            //sqMail myMail = new sqMail(Recipients:r, From:"yaron.facebook",MailSubject:"try");
            //myMail.sendThroughSMTP();
            /*
            sqResult res = new sqResult(
                new List<List<dynamic>>() 
                { 
                    new List<object>() { @"עברית", "heder2" },
                    new List<object>() { "header1", "heder3" } 
                },
                new List<List<dynamic>>()
                { 
                    new List<object>() { @"עוד אחת", "tryHarder" },
                    new List<object>() { "try1", "tryHarder2" }
                }
                    );
            FileStream fs;
            fs = new FileStream(@"c:\temp\data3.txt", FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(fs);
            writer.Write(res.makeHtmlMessage("bla"));
            writer.Close();
            byte[] h = new hash().GetHash(res);
            byte[] h2 = new hash().GetHash("bla1");
            byte[] h3 = new hash().GetHash("bla");
            IStructuralEquatable e = h;
            if (e.Equals(h2, StructuralComparisons.StructuralEqualityComparer))
                Console.WriteLine("true");
            else
                Console.WriteLine("false");
            Console.ReadLine();
            fs.Close();

/*
            String sqlCmd;
            SqlConnection myConn = new SqlConnection("Server=localhost;Integrated security=SSPI;database=SqlADataBase");

            //str = "DROP DATABASE SqlADataBase"; 

            /*str = "CREATE DATABASE SqlADataBase ON PRIMARY " +
                "(NAME = SQLA_Data, " +
                @"FILENAME = 'C:\temp\SqlADataBaseData.mdf', " +
                "SIZE = 6MB, MAXSIZE = 12MB, FILEGROWTH = 10%) " +
                "LOG ON (NAME = SQLA_Log, " +
                @"FILENAME = 'C:\temp\SqlADataBaseLog.ldf', " +
                "SIZE = 5MB, " +
                "MAXSIZE = 10MB, " +
                "FILEGROWTH = 10%)";
            //sqlCmd = "CREATE TABLE table_name(column_name1 int)";

            sqlCmd = "INSERT INTO sqDBList (Type, Name,Port) " +
                     "VALUES ('mssql', 'try',1234); ";


            SqlCommand myCommand = new SqlCommand(sqlCmd, myConn);
            try
            {
                myConn.Open();
                myCommand.ExecuteNonQuery();
                Console.WriteLine("DataBase is Created Successfully");
                Console.ReadLine();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
            finally
            {
                if (myConn.State == ConnectionState.Open)
                {
                    myConn.Close();
                }
            }
*/
        
 
